import { IParam, IContext, DataServiceBase, IHttpResponse } from '@/core';
import { Sys_organizational_unitDTOHelp } from '../dto-help/sys-organizational-unit-helps';
import { Sys_organizational_unit } from '../sys-organizational-unit';
import Sys_organizational_unitKeys from '../sys-organizational-unit-keys';

/**
 * 组织单元服务对象基类
 * 基于template/src/api/{{appEntities}}/data-service/{{appEntities}}-service-base.ts.hbs生成
 *
 * @export
 * @class Sys_organizational_unit
 * @extends {EntityBaseService}
 */
export class Sys_organizational_unitServiceBase extends DataServiceBase<Sys_organizational_unit> {
	/**
	 * 存储模式
	 *
	 * @type {(0 | 1 | 3 | 4)} 无本地存储 | 仅本地存储 | 本地及远程存储 | DTO无存储
	 * @memberof Sys_organizational_unitServiceBase
	 */
	readonly storageMode: number = 0;

	/**
	 * Creates an instance of Sys_organizational_unitServiceBase.
	 *
	 * @param {*} [opts={}]
	 * @memberof Sys_organizational_unitServiceBase
	 */
	constructor(opts: IParam = {}) {
		super(opts, 'Sys_organizational_unit');
	}

	/**
	 * 初始化基础数据
	 *
	 * @protected
	 * @memberof Sys_organizational_unitServiceBase
	 */
	protected initBasicData() {
		this.systemName = 'uaa';
		this.appName = 'web';
		this.appEntityName = 'sys_organizational_unit';
		this.appEntityCodeName = 'sys_organizational_unit';
		this.appEntityCodeNames = 'sys_organizational_units';
		this.appEntityKeyCodeName = 'id';
		this.appEntityTextCodeName = 'ou';
	}

	/**
	 * 构造实体
	 *
	 * @protected
	 * @param {IParam} data
	 * @param {IContext} [context]
	 * @return {*} { Sys_organizational_unit }
	 * @memberof Sys_organizational_unitServiceBase
	 */
	protected newEntity(
		data: IParam,
		context: IContext = {}
	): Sys_organizational_unit {
		return new Sys_organizational_unit(data);
	}

	/**
	 * 过滤当前实体服务，标准接口数据
	 *
	 * @return {*}  {*}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	protected filterEntityData(entity: Sys_organizational_unit): any {
		const data: any = {};
		Sys_organizational_unitKeys.forEach((key) => {
			if (entity[key] !== void 0) {
				data[key] = entity[key];
			}
		});
		return data;
	}

	/**
	 * CheckKey
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async CheckKey(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/checkkey`;
		data = await Sys_organizational_unitDTOHelp.get(context, data);
		const res = await this.http.post(requestPath, data);
		return res;
	}

	/**
	 * Create
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async Create(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units`;
		if (!data.srffrontuf || data.srffrontuf != 1) {
			data.id = null;
		}
		if (data.srffrontuf != null) {
			delete data.srffrontuf;
		}
		data = await Sys_organizational_unitDTOHelp.get(context, data);
		const res = await this.http.post(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.set(context, res.data);
		return res;
	}

	/**
	 * Get
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async Get(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/${encodeURIComponent(
			context['sys_organizational_unit']
		)}`;
		const res = await this.http.get(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.set(context, res.data);
		return res;
	}

	/**
	 * GetDraft
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async GetDraft(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/getdraft`;
		data['sys_organizational_unit'] = undefined;
		data['id'] = undefined;
		const res = await this.http.get(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.set(context, res.data);
		return res;
	}

	/**
	 * Items
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async Items(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/${encodeURIComponent(
			context['sys_organizational_unit']
		)}/items`;
		data = await Sys_organizational_unitDTOHelp.get(context, data);
		const res = await this.http.post(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.set(context, res.data);
		return res;
	}

	/**
	 * Remove
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async Remove(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/${encodeURIComponent(
			context['sys_organizational_unit']
		)}`;
		const res = await this.http.delete(requestPath, data);
		return res;
	}

	/**
	 * Save
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async Save(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/${encodeURIComponent(
			context['sys_organizational_unit']
		)}/save`;
		data = await Sys_organizational_unitDTOHelp.get(context, data);
		const res = await this.http.post(requestPath, data);
		return res;
	}

	/**
	 * Update
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async Update(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/${encodeURIComponent(
			context['sys_organizational_unit']
		)}`;
		data = await Sys_organizational_unitDTOHelp.get(context, data);
		const res = await this.http.put(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.set(context, res.data);
		return res;
	}

	/**
	 * FetchDefault
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async FetchDefault(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/fetchdefault`;
		const res = await this.http.post(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.toDataObjArray(
			context,
			res.data
		);
		return res;
	}

	/**
	 * FetchMyOrg
	 *
	 * @param {IParam} [context={}]
	 * @param {IParam | IParam[]} [data]
	 * @returns {Promise<any>}
	 * @memberof Sys_organizational_unitServiceBase
	 */
	public async FetchMyOrg(
		context: IContext = {},
		data: IParam | IParam[]
	): Promise<any> {
		data = data ? data : {};
		let requestPath = '';
		requestPath = `/sys_organizational_units/fetchmyorg`;
		const res = await this.http.post(requestPath, data);
		res.data = await Sys_organizational_unitDTOHelp.toDataObjArray(
			context,
			res.data
		);
		return res;
	}
}
